package zuk.clipvuiviet.clipadapter.fragment;



import java.util.ArrayList;
import java.util.List;

import zuk.clipvuiviet.ApiYoutube;
import zuk.clipvuiviet.R;
import zuk.clipvuiviet.clipadapter.Clip;
import zuk.clipvuiviet.clipadapter.ClipAdapter;
import zuk.clipvuiviet.getdata.SetList;
import zuk.clipvuiviet.getvideo.GetVideo;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
@SuppressLint("ValidFragment")
public class FragmentClip extends ListFragment {
	ClipAdapter adapter;
	List<Clip> listClips = new ArrayList<Clip>();
	String tab;
	int page;
	ListView listview;
	ProgressBar progressbar ;

	public FragmentClip(String tab) {
		this.tab = tab;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		progressbar = new ProgressBar(getActivity());
		return inflater.inflate(R.layout.fragment_clip, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		page= 1;
		listClips = new SetList().setlist(tab, Integer.toString(page));
		listview = getListView();
		listview.addFooterView(progressbar);
		if (listClips != null) {
			adapter = new ClipAdapter(getActivity(), R.layout.clip,tab,listClips);
			setListAdapter(adapter);
		} else {
			Toast.makeText(getActivity(), "Error 503 or 404", Toast.LENGTH_SHORT).show();
		}
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		
		String video;
		if(listClips.get(position).getuTube()!=null){
			video = listClips.get(position).getuTube();
		}else{
			video = new GetVideo().getvideo(listClips.get(position).getId());
		}
//		startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("vnd.youtube:" + video)));
		if (video == null) {
			Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
		}
		Intent i = new Intent(getActivity(), ApiYoutube.class);
		i.putExtra("key", video);

		startActivity(i);
//		Toast.makeText(getActivity(), "Click : " + Integer.toString(position), Toast.LENGTH_SHORT).show();
		super.onListItemClick(l, v, position, id);
	}

	
}
